<?php
/**
 * 路径测试文件
 * 用于测试wp-load.php加载路径问题
 */

// 禁用错误显示，记录到日志文件
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/logs/php_errors.log');

echo "<h1>WordPress路径测试</h1>";

// 确保日志目录存在
$log_dir = __DIR__ . '/logs';
if (!file_exists($log_dir)) {
    mkdir($log_dir, 0777, true);
}

// 记录服务器信息
echo "<h2>服务器信息</h2>";
echo "<pre>";
echo "当前脚本: " . __FILE__ . "\n";
echo "当前目录: " . __DIR__ . "\n";
echo "文档根目录: " . $_SERVER['DOCUMENT_ROOT'] . "\n";
echo "</pre>";

// 测试不同的WordPress加载路径
$paths_to_try = array(
    '标准相对路径' => '../../wp-load.php',
    'dirname函数' => dirname(dirname(dirname(__FILE__))) . '/wp-load.php',
    'DOCUMENT_ROOT' => $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php',
    '硬编码绝对路径' => '/www/wwwroot/www.ailingxi.cn/wp-load.php',
);

echo "<h2>路径测试结果</h2>";
echo "<table border='1' cellpadding='5'>";
echo "<tr><th>路径描述</th><th>完整路径</th><th>文件存在</th><th>是否可读</th></tr>";

foreach ($paths_to_try as $desc => $path) {
    $full_path = $path;
    if ($desc == '标准相对路径') {
        $full_path = __DIR__ . '/' . $path;
    }
    $exists = file_exists($full_path) ? '是' : '否';
    $readable = is_readable($full_path) ? '是' : '否';
    
    echo "<tr>";
    echo "<td>$desc</td>";
    echo "<td>$full_path</td>";
    echo "<td>$exists</td>";
    echo "<td>$readable</td>";
    echo "</tr>";
}

echo "</table>";

// 尝试最优路径加载WordPress
echo "<h2>尝试加载WordPress</h2>";

foreach ($paths_to_try as $desc => $path) {
    $full_path = $path;
    if ($desc == '标准相对路径') {
        $full_path = __DIR__ . '/' . $path;
    }
    
    if (file_exists($full_path)) {
        echo "<p>尝试加载: $desc ($full_path)</p>";
        try {
            require_once($full_path);
            echo "<p style='color:green;font-weight:bold;'>加载成功！</p>";
            
            // 验证WordPress是否正确加载
            if (function_exists('wp_get_current_user')) {
                echo "<p>WordPress函数可用: wp_get_current_user()</p>";
                echo "<p>WordPress版本: " . get_bloginfo('version') . "</p>";
            } else {
                echo "<p style='color:red;'>警告: WordPress核心函数不可用!</p>";
            }
            
            break;
        } catch (Exception $e) {
            echo "<p style='color:red;'>加载失败: " . $e->getMessage() . "</p>";
        }
    }
}

// 输出系统路径信息
echo "<h2>系统路径信息</h2>";
echo "<pre>";
echo "PHP include_path: " . get_include_path() . "\n";
echo "</pre>";

// 显示文件列表
echo "<h2>目录内容</h2>";

$parent_dir = dirname(dirname(dirname(__DIR__)));
echo "<h3>WordPress根目录($parent_dir):</h3>";
echo "<pre>";
$files = scandir($parent_dir);
foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        echo $file . (is_dir($parent_dir . '/' . $file) ? '/' : '') . "\n";
    }
}
echo "</pre>";

$plugin_dir = dirname(dirname(__DIR__));
echo "<h3>插件目录($plugin_dir):</h3>";
echo "<pre>";
$files = scandir($plugin_dir);
foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        echo $file . (is_dir($plugin_dir . '/' . $file) ? '/' : '') . "\n";
    }
}
echo "</pre>";

// 记录成功信息
echo "<p>测试完成</p>";
file_put_contents($log_dir . '/path-test.log', date('Y-m-d H:i:s') . ' - 路径测试完成' . "\n", FILE_APPEND); 